crypto: fix TLSWrap use-after-free on pending write#62967
Closed
alexkozy wants to merge 1 commit intonodejs:mainfrom
Closed
crypto: fix TLSWrap use-after-free on pending write#62967alexkozy wants to merge 1 commit intonodejs:mainfrom
alexkozy wants to merge 1 commit intonodejs:mainfrom
Conversation
Collaborator
|
Review requested:
|
EncOut() passes pointers from the enc_out_ BIO internal buffer to the underlying stream's uv_write(). write_size_ is non-zero while that write is in flight. Calling ssl_.reset() frees the SSL context and its BIOs, turning those pointers into dangling references. When libuv completes the write it accesses freed memory (SIGSEGV). Use ssl_.release() instead of ssl_.reset() when write_size_ != 0 so the BIO data stays alive for the in-flight uv_write. This is a bounded leak (one SSL context per socket destroyed with in-flight writes) that prevents a segfault. Also move RemoveStreamListener() before SSL cleanup so the underlying stream cannot call back into the TLSWrap after its SSL state is gone. Refs: nodejs#62393 Made-with: Cursor
c23571f to
afcee0e
Compare
Member
Author
|
Opened this one by accident; I should open an issue instead, since the fix here works, but it leaks the bio data structure. |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #62967 +/- ##
==========================================
- Coverage 91.49% 89.66% -1.83%
==========================================
Files 356 706 +350
Lines 150784 219397 +68613
Branches 23768 42062 +18294
==========================================
+ Hits 137955 196730 +58775
- Misses 12557 14593 +2036
- Partials 272 8074 +7802
🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Fix use-after-free in
TLSWrap::Destroy()when an underlying stream write is still in flight.EncOut()passes pointers from theenc_out_BIO internal buffer to the underlying stream'suv_write().write_size_is non-zero while that write is in flight. Callingssl_.reset()frees the SSL context and its BIOs, turning those pointers into dangling references. When libuv completes the write it accesses freed memory (SIGSEGV).This change:
ssl_.release()instead ofssl_.reset()whenwrite_size_ != 0so the BIO data stays alive for the in-flightuv_write. This is a bounded leak (one SSL context per socket destroyed with in-flight writes) that prevents a segfault.RemoveStreamListener()before SSL cleanup so the underlying stream cannot call back into the TLSWrap after its SSL state is gone.Test plan
python3 tools/test.py --mode=release test/parallel/test-tls-destroy-ssl-with-pending-write test/parallel/test-tls-destroy-whilst-write test/parallel/test-tls-socket-destroy test/parallel/test-tls-transport-destroy-after-own-gc